'use strict';

const Controller = require('egg').Controller;
const w=require("ws")

const fs=require("fs")
const path = require("path")

// const path = require("path")

// const fs = require("fs")

// var svgCaptcha = require('svg-captcha');
// const { url } = require('inspector');

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = '騎蝸牛追导弹';
    console.log(w);
  }
  //获取医生详细信息
  async docin() {
    let re=this.ctx.request.query.id
    let sql=`SELECT * FROM goods WHERE id=${re}`
    let res1 = await this.app.mysql.query(sql)
    this.ctx.body=res1
  }

  //获取用户id
  async getuserid() {
    // let res=this.ctx.cookies.get("uid")
    let res=this.ctx.session.uid
    
    // console.log(res);
    if (res==undefined) {
      this.ctx.body=0
    }else{
      this.ctx.body=res
    }
    
  }
  //创建用户问题表
  async productud() {
    
    let res = this.ctx.request.body
    
    let res1 = this.ctx.request.files
    // console.log(res,res1);
    for (let i = 0; i < res1.length; i++) {
     let oldpath=res1[i].filepath
    //  console.log(oldpath,111);
     let newpath=__dirname+"/../../public/prombleimg/"+path.basename(oldpath)
      fs.copyFile(oldpath,newpath,function(err){
        fs.unlink(oldpath,(err)=>{})
      })
      res[res1[i].field]="/public/user/"+path.basename(oldpath)
      
    }
    let username=this.ctx.session.username
    // console.log(res);
    //添加到数据库
    let sql=`INSERT INTO promble (uid,did,prob,photos0,photos1,photos2,username) VALUE (${res.uid},${res.did},"${res.promble}","${res.photos0}","${res.photos1}","${res.photos2}","${username}")`
    let res2=await this.ctx.app.mysql.query(sql)
    this.ctx.body={
      code:200,
      info:"询问成功"
    }
  }
  //获取问题表
  async getprob() {
   let did = this.ctx.request.query.did
   let count = this.ctx.request.query.count
    // console.log(did);
    // console.log(res);
    this.ctx.body=await this.ctx.service.home.getProb({did:did,start:0,end:count})
  }
  //判断该用户是否关注该医生
  async isgz() {
    let uid=this.ctx.cookies.get("uid")
    let did=this.ctx.request.query.did
    if (!uid) {
      this.ctx.body=[]
    }else{
      let sql=`SELECT * FROM  ueranddoctor_xyl WHERE uid=${uid} AND did=${did}`
      let res=await this.app.mysql.query(sql)
      this.ctx.body=res
    }
  }
  // //改变isgt
  // async updataisgz() {
  //   let iso=this.ctx.request.query.isgz

  //   let uid=this.ctx.cookies.get("uid")
      
  //     let sql=`UPDATE user_lx SET isgz=${iso} WHERE uid=${uid}`
  //     let res=await this.app.mysql.query(sql)
  //     if (iso==1) {
  //       this.ctx.body={code:200,info:"关注成功"}
  //     }else{
  //       this.ctx.body={code:200,info:"取消关注"}
  //     }
  // }
  async setud() {
    let did = this.ctx.request.query.did
    let uid=this.ctx.cookies.get("uid")
    let sql=`INSERT INTO ueranddoctor_xyl (uid,did) VALUE (${uid},${did})`
    this.app.mysql.query(sql)
    this.ctx.body="ok"
   }
   async deleteud() {
    let did = this.ctx.request.query.did
    let sql=`DELETE FROM ueranddoctor_xyl WHERE did=${did}`
    this.app.mysql.query(sql)
    this.ctx.body="ok"
  
  }
 //获取问题表长度
 async getproblen() {
    let sql=`SELECT * FROM promble `
    let res=await this.app.mysql.query(sql)
    this.ctx.body=res
 }
 //写评论到数据库
 async creatcomment() {
  let did=this.ctx.request.query.did
  let content=this.ctx.request.query.content
  let uid=this.ctx.cookies.get("uid")
  let username=this.ctx.session.username
  // console.log(uid,did,username,content);
  let sql=`INSERT INTO comment (uid,did,username,content) VALUE (${uid},${did},"${username}","${content}")`
  this.app.mysql.query(sql)
  this.ctx.body={
    code:200,
    info:"写入成功"
  }
}
 //获取评论表
 async getcomment() {
  let did = this.ctx.request.query.did
  let count = this.ctx.request.query.count
   this.ctx.body=await this.ctx.service.home.getcomment({did:did,start:0,end:count})
 }

 //获取评论表长度
 async getcommentlen() {
  let sql=`SELECT * FROM comment `
  let res=await this.app.mysql.query(sql)
  this.ctx.body=res
}
}


  

  







module.exports = HomeController;
